package com.example.androidkt.fourth

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import com.example.androidkt.R
import com.example.androidkt.databinding.ActivityLiveData1Binding
import com.example.androidkt.databinding.ActivityMain2Binding

class LiveData1Activity : AppCompatActivity() {
/**
 * setValue 主线程使用
 * postValue 子线程使用
 * */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        var testValueData = MutableLiveData<String>()

        val binding = ActivityLiveData1Binding.inflate(layoutInflater)
        setContentView(binding.root)

        val liveDataFragment = LiveDataFragment()
        supportFragmentManager
            .beginTransaction()
            .add(R.id.fragmet_framelayout, liveDataFragment)
            .commit()



        binding.btnCrateFg.setOnClickListener {
            supportFragmentManager
                .beginTransaction()
                .attach(liveDataFragment)
                .commit()
        }

        binding.btnDestroyFg.setOnClickListener {
            supportFragmentManager
                .beginTransaction()
                .detach(liveDataFragment)
                .commit()
        }
        binding.btnChangeFg.setOnClickListener {
            testValueData.value = "当前livedata得值 ：${(1000..9999).random()}"
        }

        testValueData.observe(this){
            binding.tvLiveDataAc.text = it
        }




    }


}